package com.menu.web.controller.reception;

import com.menu.common.annotation.Log;
import com.menu.common.core.controller.BaseController;
import com.menu.common.core.domain.AjaxResult;
import com.menu.common.core.page.TableDataInfo;
import com.menu.common.enums.BusinessType;
import com.menu.common.enums.DataSourceType;
import com.menu.common.utils.StringUtils;
import com.menu.common.utils.poi.ExcelUtil;
import com.menu.framework.datasource.DynamicDataSourceContextHolder;
import com.menu.system.domain.ReceptionRole;
import com.menu.system.service.IreceptionMenuService;
import com.menu.system.service.IreceptionRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色信息Controller
 *
 * @author ruoyi
 * @date 2024-06-06
 */
@RestController
@RequestMapping("/system/receptionRole")
public class receptionRoleController extends BaseController {
    @Autowired
    private IreceptionRoleService receptionRoleService;
    @Autowired
    private IreceptionMenuService receptionMenuService;

    /**
     * 查询角色信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:receptionRole:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReceptionRole receptionRole) {
        startPage();
        List<ReceptionRole> list = receptionRoleService.selectreceptionRoleList(receptionRole);
        return getDataTable(list);
    }

    /**
     * 导出角色信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:receptionRole:export')")
    @Log(title = "角色信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReceptionRole receptionRole) {
        List<ReceptionRole> list = receptionRoleService.selectreceptionRoleList(receptionRole);
        ExcelUtil<ReceptionRole> util = new ExcelUtil<ReceptionRole>(ReceptionRole.class);
        util.exportExcel(response, list, "角色信息数据");
    }

    /**
     * 获取角色信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:receptionRole:query')")
    @GetMapping(value = "/{roleId}")
    public AjaxResult getInfo(@PathVariable("roleId") Long roleId) {
        AjaxResult ajaxResult = new AjaxResult();
        ReceptionRole receptionRole = receptionRoleService.selectreceptionRoleByRoleId(roleId);
        ajaxResult.put(AjaxResult.DATA_TAG, receptionRole);
        List<Long> idsList = Arrays.asList(receptionRole.getRoleKey().split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());

        ajaxResult.put("roles", idsList);
        ajaxResult.put("menus", receptionMenuService.selectreceptionMenuList(null));
        return ajaxResult;
    }

    /**
     * 新增角色信息
     */
    @PreAuthorize("@ss.hasPermi('system:receptionRole:add')")
    @Log(title = "角色信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReceptionRole receptionRole) {

        receptionRole.setRoleKey(StringUtils.join(receptionRole.getRolds(), ","));
        logger.info("数据源" + DynamicDataSourceContextHolder.getDataSourceType());
        receptionRoleService.insertreceptionRole(receptionRole);
        DynamicDataSourceContextHolder.setDataSourceType(DynamicDataSourceContextHolder.getDataSourceType() == DataSourceType.MASTER.name() ? DataSourceType.SLAVE.name() : DataSourceType.MASTER.name());
        logger.info("数据源" + DynamicDataSourceContextHolder.getDataSourceType());
        return toAjax(receptionRoleService.insertreceptionRole(receptionRole));
    }

    /**
     * 修改角色信息
     */
    @PreAuthorize("@ss.hasPermi('system:receptionRole:edit')")
    @Log(title = "角色信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReceptionRole receptionRole) {
        receptionRole.setRoleKey(StringUtils.join(receptionRole.getRolds(), ","));
        logger.info("数据源" + DynamicDataSourceContextHolder.getDataSourceType());
        receptionRoleService.updatereceptionRole(receptionRole);
        DynamicDataSourceContextHolder.setDataSourceType(DynamicDataSourceContextHolder.getDataSourceType() == DataSourceType.MASTER.name() ? DataSourceType.SLAVE.name() : DataSourceType.MASTER.name());
        logger.info("数据源" + DynamicDataSourceContextHolder.getDataSourceType());
        return toAjax(receptionRoleService.updatereceptionRole(receptionRole));
    }

    /**
     * 删除角色信息
     */
    @PreAuthorize("@ss.hasPermi('system:receptionRole:remove')")
    @Log(title = "角色信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public AjaxResult remove(@PathVariable Long[] roleIds) {
        logger.info("数据源" + DynamicDataSourceContextHolder.getDataSourceType());
        receptionRoleService.deletereceptionRoleByRoleIds(roleIds);
        DynamicDataSourceContextHolder.setDataSourceType(DynamicDataSourceContextHolder.getDataSourceType() == DataSourceType.MASTER.name() ? DataSourceType.SLAVE.name() : DataSourceType.MASTER.name());
        logger.info("数据源" + DynamicDataSourceContextHolder.getDataSourceType());
        return toAjax(receptionRoleService.deletereceptionRoleByRoleIds(roleIds));
    }
}
